[PATCH] QXmlStreamReader: make fastScanName() indicate parsing status to callers
authorAhmad Samir <a.samirh78@gmail.com>
Thu, 22 Jun 2023 12:56:07 +0000 (15:56 +0300)
committerRaspbian forward porter <root@raspbian.org>
Wed, 7 Feb 2024 11:12:42 +0000 (11:12 +0000)
commit4808544fb037b8e7fe66110435a15314797da6c3
tree8e2851095b862eebb06880602c2e7d6011328e9c
parenta0c8520457f04f82afe0a2b9d8b2888a87287770
[PATCH] QXmlStreamReader: make fastScanName() indicate parsing status to callers

This fixes a crash while parsing an XML file with garbage data, the file
starts with '<' then garbage data:
- The loop in the parse() keeps iterating until it hits "case 262:",
  which calls fastScanName()
- fastScanName() iterates over the text buffer scanning for the
  attribute name (e.g. "xml:lang"), until it finds ':'
- Consider a Value val, fastScanName() is called on it, it would set
  val.prefix to a number > val.len, then it would hit the 4096 condition
  and return (returned 0, now it returns the equivalent of
  std::null_opt), which means that val.len doesn't get modified, making
  it smaller than val.prefix
- The code would try constructing an XmlStringRef with negative length,
  which would hit an assert in one of QStringView's constructors

Add an assert to the XmlStringRef constructor.

Add unittest based on the file from the bug report.

Later on I will replace FastScanNameResult with std::optional<qsizetype>
(std::optional is C++17, which isn't required by Qt 5.15, and we want to
backport this fix).

Credit to OSS-Fuzz.

Fixes: QTBUG-109781
Fixes: QTBUG-114829
Pick-to: 6.6 6.5 6.2 5.15
Change-Id: I455a5eeb47870c2ac9ffd0cbcdcd99c1ae2dd374
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Gbp-Pq: Name cve-2023-37369.diff
src/corelib/serialization/qxmlstream.cpp
src/corelib/serialization/qxmlstream.g
src/corelib/serialization/qxmlstream_p.h
src/corelib/serialization/qxmlstreamparser_p.h
tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp